package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * ClassName: DishController
 * Package: com.sky.controller.admin
 * Description:
 *
 * @Author Dylan_Jun
 * @Create 2024/1/31 13:49
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dto){
        log.info("新增菜品，{}", dto);
        dishService.insert(dto);
        String key = "dish_" + dto.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> PageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询, {}", dishPageQueryDTO);
        PageResult res = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(res);
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result DeleteByids(@RequestParam List<Long> ids){
        log.info("批量删除id为{}的菜品", ids);
        dishService.deleteByids(ids);
        cleanCache("dish_*");
        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品, {}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> listByCategoryId(@RequestParam Long categoryId){
        log.info("根据分类id查询菜品,{}", categoryId);
        List<Dish> dishes = dishService.listByCategoryId(categoryId);
        return Result.success(dishes);
    }

    @ApiOperation("更新菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        log.info("更新菜品信息，{}", dto);
        dishService.update(dto);
        cleanCache("dish_*");
        return Result.success();
    }

    @ApiOperation("菜品状态设置")
    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status, Long id){
        log.info("菜品状态设置,{},{}", status, id);
        dishService.setStatus(status, id);
        cleanCache("dish_*");
        return Result.success();
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
